CS Engineering Gyan

Array in Data Structure

An array is one of the most basic and commonly used data structures in computer science. It is used to store a collection of elements of the same data type in a single variable. All elements in an array are stored in continuous memory locations, which means they are placed next to each other in the computer’s memory.

Each element in an array is identified by a unique number called an index. The index usually starts from 0, so the first element is accessed using index 0, the second using index 1, and so on. Because of this indexing system, accessing any element in an array is very fast and efficient.

Arrays make programs easier to understand and manage because they allow multiple values to be grouped under a single name. For example, instead of creating separate variables for storing marks of students, an array can store all marks together in an organized manner.

Arrays are especially useful when the amount of data is fixed or known in advance. They are widely used in tasks such as storing lists of numbers, handling strings, managing tables of data, and implementing other data structures like stacks and queues.

Due to their simplicity, speed, and ease of use, arrays form the foundation of many algorithms and advanced data structures in programming.


Characteristics of Array


Types of Array

1. One-Dimensional Array

A one-dimensional array is the simplest and most commonly used form of array in programming. In this type of array, data elements are stored in a single straight line, one after another in continuous memory locations. Each element is accessed using only one index value.

You can think of a one-dimensional array as a row of boxes, where each box holds a value and is identified by a number called its index. The index usually starts from 0, so the first element is accessed using index 0, the second using index 1, and so on.

One-dimensional arrays are widely used to store simple lists of related data. For example, they can be used to store marks of students in a class, prices of products in a shop, roll numbers of students, or temperatures recorded over several days.

This type of array allows very fast access to data because the memory location of any element can be calculated directly using its index. However, the size of a one-dimensional array is usually fixed at the time of declaration, so the number of elements must be decided in advance.

Syntax (C Language):

data_type array_name[size];

Example:

int marks[5] = {65, 78, 90, 82, 74};

Program:

#include <stdio.h>

int main() {
    int arr[5] = {10, 20, 30, 40, 50};
    int i;

    for(i = 0; i < 5; i++) {
        printf("%d ", arr[i]);
    }
    return 0;
}

2. Two-Dimensional Array

A two-dimensional array is a type of array that stores data in the form of rows and columns, making it similar to a table, grid, or matrix. Instead of using a single index, each element in a two-dimensional array is accessed using two index values: one for the row and one for the column.

You can imagine a two-dimensional array like a spreadsheet or a classroom seating chart, where data is arranged neatly in rows and columns. This structure is very useful when data naturally fits into a tabular format.

Two-dimensional arrays are commonly used in mathematical operations such as matrix addition, subtraction, and multiplication. They are also widely used to store and process tabular data like student records, examination marks, employee salary tables, and game boards.

Just like one-dimensional arrays, the elements of a two-dimensional array are stored in continuous memory locations, which allows efficient access using index values. However, the size of rows and columns is usually fixed at the time of declaration.

Syntax:

data_type array_name[rows][columns];

Example:

int matrix[2][3] = {
    {1, 2, 3},
    {4, 5, 6}
};

Program:

#include <stdio.h>

int main() {
    int mat[2][2] = {{1,2},{3,4}};
    int i, j;

    for(i = 0; i < 2; i++) {
        for(j = 0; j < 2; j++) {
            printf("%d ", mat[i][j]);
        }
        printf("\n");
    }
    return 0;
}

3. Multi-Dimensional Array

Multi-dimensional arrays contain more than two dimensions and are used in advanced applications such as scientific simulations and 3D graphics.

int data[2][2][2];

Basic Operations on Array


Advantages of Array

Disadvantages of Array


Real-Life Examples of Array

Conclusion

Arrays are the foundation of many data structures. A clear understanding of arrays helps learners grasp advanced concepts such as linked lists, stacks, queues, and trees more effectively.

← Previous: Array in Data Structure Next: One-Dimensional Array →
Home Visit Our YouTube Channel